#include "agencymgr.h"
#include "DbPart.h"
#include "minutelisten.h"



AgencyMgr::AgencyMgr()
{
}
AgencyMgr::~AgencyMgr()
{
}

stAgency* AgencyMgr::Get(const std::string& agency_account)
{
    auto it_name = m_name2id_group.find(agency_account);
    if (it_name != m_name2id_group.end())
    {   
        auto it = m_agency_infos.find(it_name->second);
        if (it != m_agency_infos.end())
        {   
            return &it->second;
        }
    }

    // 加载数据库数据
    stAgency agency;
    agency.name = agency_account;
    if (!CDbPart::Instance()->GetAgency(agency))
    {
        return nullptr;
    }
    m_agency_infos[agency.id] = std::move(agency);
    m_name2id_group[agency.name] = agency.id;

    return &m_agency_infos[agency.id];
}

stAgency* AgencyMgr::Get(const std::uint32_t agency_id)
{
    auto it = m_agency_infos.find(agency_id);
    if (it != m_agency_infos.end())
    {   
        return &it->second;
    }
    // 加载数据库数据
    stAgency agency;
    agency.id = agency_id;
    if (!CDbPart::Instance()->GetAgency(agency))
    {
        return nullptr;
    }
    m_agency_infos[agency.id] = std::move(agency);
    m_name2id_group[agency.name] = agency.id;

    return &m_agency_infos[agency_id];
}

stAgency* AgencyMgr::RefreshGet(const std::uint32_t agency_id)
{
    // 加载数据库数据
    stAgency agency;
    agency.id = agency_id;
    if (!CDbPart::Instance()->GetAgency(agency))
    {
        m_agency_infos.erase(agency_id);
        return nullptr;
    }
    m_agency_infos[agency.id] = std::move(agency);
    m_name2id_group[agency.name] = agency.id;

    return &m_agency_infos[agency_id];
}







